package com.stone.boot.pipeline.api.controller.v1;

import com.stone.boot.pipeline.app.service.PipelineNodeService;
import com.stone.boot.pipeline.config.StonePipelineSwaggerTagConfig;
import com.stone.boot.pipeline.domain.entity.PipelineNode;
import com.stone.boot.pipeline.domain.repository.PipelineNodeRepository;
import com.stone.starter.core.base.BaseController;
import com.stone.starter.core.util.result.Results;
import io.choerodon.core.iam.ResourceLevel;
import io.choerodon.mybatis.pagehelper.PageHelper;
import io.choerodon.mybatis.pagehelper.annotation.SortDefault;
import io.choerodon.mybatis.pagehelper.domain.PageRequest;
import io.choerodon.swagger.annotation.Permission;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.hzero.mybatis.helper.SecurityTokenHelper;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collections;
import java.util.List;

/**
 * 流程器节点控制器
 *
 * @author Mr_wenpan@163.com 2021/08/05 22:19
 */
@RestController("pipelineNodeController.v1")
@RequestMapping("/v1/pipeline-node")
@Api(tags = StonePipelineSwaggerTagConfig.PIPELINE_NODE)
public class PipelineNodeController extends BaseController {

    private final PipelineNodeRepository pipelineNodeRepository;

    private final PipelineNodeService pipelineNodeService;

    public PipelineNodeController(PipelineNodeRepository pipelineNodeRepository,
                                  PipelineNodeService pipelineNodeService) {
        this.pipelineNodeRepository = pipelineNodeRepository;
        this.pipelineNodeService = pipelineNodeService;
    }

    @GetMapping
    @ApiOperation(value = "流程器节点列表")
    public ResponseEntity<?> list(PipelineNode pipelineNode,
                                  @ApiIgnore @SortDefault(value = PipelineNode.FIELD_ID) PageRequest pageRequest) {
        return Results.success(PageHelper.doPage(pageRequest.getPage(), pageRequest.getSize(),
                () -> pipelineNodeRepository.listPipelineNode(pipelineNode)));
    }

    @GetMapping("/list")
    @ApiOperation(value = "流程器节点列表-无分页")
    public ResponseEntity<?> listAll(PipelineNode pipelineNode) {
        return Results.success(pipelineNodeRepository.listPipelineNode(pipelineNode));
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "流程器节点明细")
    public ResponseEntity<?> detail(@PathVariable Long id) {
        return Results.success(pipelineNodeRepository.selectByPrimaryKey(id));
    }

    @PostMapping
    @ApiOperation(value = "批量新增/修改流程器节点")
    public ResponseEntity<?> create(@RequestBody List<PipelineNode> pipelineNodes) {
        int errorCount = pipelineNodeService.batchMerge(pipelineNodes);
        if (0 == errorCount) {
            return Results.success(Collections.emptyList());
        } else {
            String resultString = "下个节点行为和决策类型不可重复，请检查节点数据" +
                    "，成功保存" + (pipelineNodes.size() - errorCount) + "条数据";
            return Results.success(Collections.singletonList(resultString));
        }
    }

    @PostMapping("/batch-create-and-update")
    @ApiOperation(value = "批量新增/修改流程器节点(重写)")
    public ResponseEntity<?> batchCreateAndUpdate(@RequestBody List<PipelineNode> pipelineNodes) {
        pipelineNodeService.batchCreateAndUpdate(pipelineNodes);
        return Results.success();
    }

    @PostMapping("/batch-insert")
    @ApiOperation(value = "批量新增流程器节点")
    public ResponseEntity<?> batchCreate(@RequestBody List<PipelineNode> pipelineNodes) {
        pipelineNodeService.batchCreate(pipelineNodes);
        return Results.success();
    }

    @PutMapping("/batch-update")
    @ApiOperation(value = "批量修改流程器节点")
    public ResponseEntity<?> batchUpdate(@RequestBody List<PipelineNode> pipelineNodes) {
        pipelineNodeService.batchUpdate(pipelineNodes);
        return Results.success();
    }

    @ApiOperation(value = "批量删除流程器节点")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping
    public ResponseEntity<?> remove(@RequestBody List<PipelineNode> pipelineNodes) {
        SecurityTokenHelper.validToken(pipelineNodes);
        pipelineNodeRepository.batchDeleteByPrimaryKey(pipelineNodes);
        return Results.success();
    }

    @ApiOperation(value = "批量删除流程器节点（重写）")
    @Permission(level = ResourceLevel.ORGANIZATION)
    @DeleteMapping("/batch-remove")
    public ResponseEntity<?> batchRemove(@RequestBody List<PipelineNode> pipelineNodes) {
        SecurityTokenHelper.validToken(pipelineNodes);
        pipelineNodeService.batchRemove(pipelineNodes);
        return Results.success();
    }

}
